/**
 * Created by acer on 2016/9/12.
 */
$('.head_title ul ').each(function(){
    $(this).find('li>a:eq(0)').css('color','blue')
    $(this).find('li>a:eq(1)').css('color','black')
    $(this).find('li>a:eq(2)').css('color','grey')
});
$('.head ul ').each(function(){
    $(this).find('li>a:eq(0)').css('color','blue')
});
$('.lunbo>ul').each(function(){
    $(this).find('li:eq(0)').css('background','#00B3FF')
})
$('.lunbo .neirong ').each(function(){
    $(this).find('.text:eq(0)').css({
        opacity:1,
        left:'150px'
    })
})
$('.lunbo .neirong ').each(function(){
    $(this).find('.img:eq(0)').css({
        opacity:1,
        left:'700px'
    })
})
var zhanshi=$('.lunbo');
var gundong=$('.neirong');
var time;
var index=0;
var distance=zhanshi.innerWidth();
var a=$('.lunbo>ul li')
var text= $('.lunbo .neirong .text')
var img= $('.lunbo .neirong .img')
console.log(a)
console.log(distance);
function huadong(){
    var position=gundong.position()
    var start=position.left
    var end=index*distance*(-1);
    var change=end-start;
    var timer;
    var t=0;
    var maxt=20;
    clear();
    if(index==a.length){
        $(a[0]).css("background","#00B3FF");
    }else{
        $(a[index]).css("background","#00B3FF");
    }
    timer=setInterval(function(){
            t++;
            if(t>=maxt){//当图片到达终点停止计时器
                $(text[index]).attr({
                    style:'transition-delay:0.5s;left:150px;opacity:1'
                })
                $(img[index]).attr({
                    style:'transition-delay:0.7s;left:700px;opacity:1'
                })
                var textleft=$(text[index]).css('left')
                if(textleft!="150px"){
                    $('.lunbo').disable
                }
                clearInterval(timer);
            }
            gundong.css('left',change/maxt*t+start+"px");//每个17毫秒让块移动
            if(index==5&&t>=maxt){
                gundong.css('left',0)
                index=0; //当图片到最后一张时把它瞬间切换回第一张，由于都同一张图片不会影响效果
                $(text[index]).attr({
                    style:'transition-delay:0.5s;left:150px;opacity:1'
                })
                $(img[index]).attr({
                    style:'transition-delay:0.7s;left:700px;opacity:1'
                })
            }
    },17)
}
    function right_go(){
        index+=1;
        if(index>5){
            index=0;
        }
        huadong();
    }
function left_go(){
    index-=1;
    if(index<0){
        index= a.length-1;
        gundong.css('left',(-5)*distance+"px")
    }
    huadong();
}
var auto=setInterval(right_go,5000);
$('#left').click(function(){
        left_go()
})
$('#right').click(function(){
        right_go()
})
function clear(){
    for(var i=0;i< a.length;i++){
        $(a[i]).css("background","white");
    }
    for(var i=0;i< text.length;i++){
        $(text[i]).css({
            left: '200px',
            opacity: 0
        })
    }
    for(var i=0;i< img.length;i++){
        $(img[i]).css({
            left: '780px',
            opacity: 0
        })
    }
}
for(var i=0;i< a.length;i++){
    $(a[i]).attr('id',i);
    $(a[i]).click(function(){
        debugger;
        index=parseInt($(this).attr('id'));
        huadong();
    });
}
$('.lunbo').mousemove(function(){
    clearInterval(auto);
})
$('.lunbo').mouseout(function(){
    auto=setInterval(right_go,3000)
})
